<% content_for :title do %>
  <%= "#{@movie.code} #{@movie.title}" %>
<% end %>

<% content_for :js do %>
  <script>
    $(function() {
      document.onkeydown = function(e) {
        if (e.key === "d") {
          $("a.vote-down")[0].click();
        } else if (e.key === "u") {
          $("a.vote-up")[0].click();
        } else if (e.key === "b") {
          $("a.vote-bookmark")[0].click();
        } else if (e.key === "h") {
          $("a.resource-download.HD1080")[0].click();
        }
      };
    });
  </script>
<% end %>

<h1>
  <span><%= @movie.code %></span>
  <span><%= @movie.title %></span>
</h1>

<div class="row mt-3">
  <div class="col-md-8">
    <%= render partial: '/movies/carousel', object: [@movie.cover_image] + (@movie.sample_images || []) %>
    <% if signed_in? %>
      <div class="text-center mt-3">
        <%= render partial: '/movies/voter', locals: { movie: @movie, height: 32 } %>
      </div>
    <% end %>
  </div>
  <div class="col-md-4">
    <dl>
      <% %w[actresses actress_types categories directors genres label maker series tags].each do |key| %>
        <% unless (value = @movie.read_attribute(key)).blank? %>
          <dt><%= key.titleize %></dt>
          <dd>
            <% if value.is_a? Array %>
              <ul class="list-inline">
                <% value.each do |item| %>
                  <li class="list-inline-item">
                    <%= link_to item, movies_url(fuzzy: item) %>
                  </li>
                <% end %>
              </ul>
            <% else %>
              <%= link_to value, movies_url(fuzzy: value) %>
            <% end %>
          </dd>
        <% end %>
      <% end %>
      <% %w[movie_length release_date].each do |key| %>
        <% unless (value = @movie.read_attribute(key)).blank? %>
          <dt><%= key.titleize %></dt>
          <dd><%= value %></dd>
        <% end %>
      <% end %>
      <dt>Data Source</dt>
      <dd><%= link_to @movie.page, @movie.page %></dd>
      <% if signed_in_as_admin? %>
        <dt>Wrong information?</dt>
        <dd><%= link_to 'Refresh', @movie, method: :delete, class: 'btn btn-warning', id: 'refresh' %></dd>
      <% end %>
    </dl>
  </div>
</div>

<% if signed_in_as_admin? && @movie.resources.exists? %>
  <div class="table-responsive">
    <table class="table table-striped mt-3" id="resources">
      <thead class="thead-dark">
        <tr>
          <th scope="col">Download URI</th>
          <th scope="col">Source URI</th>
          <th scope="col">Tags</th>
          <th scope="col">Actions</th>
        </tr>
      </thead>
      <tbody>
        <% @movie.resources.each do |resource| %>
          <tr>
            <td><%= link_to(
              resource.in_bt? ? 
                CGI::unescape(File.basename(resource.download_uri)) :
                resource.download_uri,
              resource,
              class: resource.tags + ['resource-download'],
            ) %></td>
            <td><%= link_to(resource.source_uri, resource.source_uri) if resource.source_uri %></td>
            <td><% resource.tags.each do |tag| %>
              <span class="badge badge-light"><%= tag %></span>
            <% end %></td>
            <td>
              <%= link_to(
                octicon(resource.in_bt? ? 'desktop-download' : 'cloud-download'),
                resource,
                class: 'btn btn-primary',
              ) %>
              <%= link_to(
                octicon(:trashcan),
                resource,
                method: :delete,
                class: 'btn btn-danger',
                data: { confirm: 'Are you sure?' }
              ) %>
            </td>
          </tr>
        <% end %>
      </tbody>
    </table>
  </div>
<% end %>
